/**
******************************************************************************
* @File:
* @Author:	Ares
* @Revision:
* @Date:
* @Brief:
******************************************************************************
* @attention
*
******************************************************************************
*/
#include "fx.h"
#include "dev.h"
#include "util.h"
#include "sys.h"
#include "conf.h"
//3.2.1
void fx_realtime_report(void)
{
	char cmd[256]={0};
	char buf[20]={0};
	cJSON *root;
	char *jmsg;
	Date_Obj *date;
	Aliyun_Obj *ali;
	
	date=&gSys.date;
	ali=&gSys.ali;
	
	sprintf(cmd,"/%s/%s/update",ali->ProductKey,ali->DeviceName);
	root=cJSON_CreateObject();
	sprintf(buf,"20%02x-%02x-%02x %02x:%02x:%02x",date->year
																											,date->month
																											,date->day
																											,date->hour
																											,date->min
																											,date->sec);
	cJSON_AddItemToObject(root,"time",cJSON_CreateString(buf));
	
	cJSON_AddItemToObject(root,"energy",cJSON_CreateNumber(fx.realtime_param.energy));
	cJSON_AddItemToObject(root,"temp",cJSON_CreateNumber(fx.realtime_param.temp));
	cJSON_AddNumberToObject(root,"power",fx.realtime_param.power);
	cJSON_AddNumberToObject(root,"runtime",fx.realtime_param.runTime);
	cJSON_AddItemToObject(root,"standy",cJSON_CreateNumber(fx.realtime_param.standy));
	cJSON_AddNumberToObject(root,"accStatus",fx.realtime_param.accStatus);
	cJSON_AddNumberToObject(root,"relayStatus",fx.realtime_param.relayStatus);
	cJSON_AddNumberToObject(root,"stragyStatus",fx.realtime_param.stragyStatus);
	at_get_csq();
	cJSON_AddNumberToObject(root,"rssi",fx.realtime_param.rssi);
	cJSON_AddNumberToObject(root,"runtime",fx.realtime_param.runTime);
	cJSON_AddNumberToObject(root,"type",0);
	
	jmsg=cJSON_Print(root);
	cJSON_Minify(jmsg);
	mqtt_pub((u8*)cmd,jmsg);
	cJSON_Delete(root);
	cJSON_free(jmsg);
}

//3.2.2
void fx_event_report(int type,char *msg)
{
	u8 cmd[256]={0};
	u8 buf[20]={0};
	cJSON *root;
	char *jmsg;
	Date_Obj *date;
	date=&gSys.date;
	
	Aliyun_Obj *ali=&gSys.ali;
	sprintf((char *)cmd,"/%s/%s/update/event",ali->ProductKey,ali->DeviceName);
	
	root=cJSON_CreateObject();
	sprintf((char*)buf,"20%02x-%02x-%02x %02x:%02x:%02x",date->year
																											,date->month
																											,date->day
																											,date->hour
																											,date->min
																											,date->sec);
	cJSON_AddItemToObject(root,"time",cJSON_CreateString((char*)buf));
	cJSON_AddNumberToObject(root,"type",type);
	if(10==type)
	{
		cJSON *event=cJSON_CreateObject();
		cJSON_AddItemToObject(event,"SIM",cJSON_CreateString((char*)gSys.gprs.sim));
		cJSON_AddItemToObject(event,"IMEI",cJSON_CreateString((char*)gSys.gprs.imei));
		cJSON_AddItemToObject(root,"event",event);
	}
	else
	{
		cJSON_AddItemToObject(root,"event",cJSON_CreateString((char*)msg));
	}
	
	jmsg=cJSON_Print(root);
	cJSON_Minify(jmsg);
	mqtt_pub(cmd,jmsg);
	cJSON_Delete(root);
	cJSON_free(jmsg);
}